#!/usr/bin/env python3
"""
PyHashCalculator 打包脚本
支持 Windows 和 macOS 系统打包
"""

import os
import sys
import subprocess
import platform

def install_requirements():
    """安装打包所需的依赖"""
    print("正在安装打包依赖...")
    subprocess.run([sys.executable, "-m", "pip", "install", "pyinstaller"], check=True)
    subprocess.run([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"], check=True)

def build_windows():
    """Windows系统打包"""
    print("正在Windows系统打包...")
    cmd = [
        "pyinstaller",
        "--clean",
        "--noconfirm",
        "--onedir",  # 单目录模式，包含所有依赖
        "--windowed",  # 不显示控制台窗口
        "--name", "PyHashCalculator",
        "--icon", "md5.ico",  # 自定义图标
        "--distpath", "./dist",
        "--workpath", "./build",
        "main.py"
    ]
    subprocess.run(cmd, check=True)
    print("Windows打包完成！可执行文件位于: dist/PyHashCalculator/PyHashCalculator.exe")

def build_macos():
    """macOS系统打包"""
    print("正在macOS系统打包...")
    cmd = [
        "pyinstaller",
        "--clean",
        "--noconfirm",
        "--onedir",  # 单目录模式
        "--windowed",
        "--name", "PyHashCalculator",
        "--icon", "md5.ico",  # 使用ico文件，PyInstaller会自动转换
        "--distpath", "./dist",
        "--workpath", "./build",
        "main.py"
    ]
    subprocess.run(cmd, check=True)
    print("macOS打包完成！可执行文件位于: dist/PyHashCalculator.app")

def build_with_spec():
    """使用spec文件打包（推荐）"""
    print("正在使用spec文件打包...")
    subprocess.run(["pyinstaller", "--clean", "--noconfirm", "build.spec"], check=True)
    
    system = platform.system()
    if system == "Windows":
        print("打包完成！可执行文件位于: dist/PyHashCalculator.exe")
    elif system == "Darwin":
        print("打包完成！可执行文件位于: dist/PyHashCalculator.app")
    else:
        print("打包完成！可执行文件位于: dist/PyHashCalculator")

def main():
    """主函数"""
    if len(sys.argv) > 1:
        if sys.argv[1] == "--windows":
            install_requirements()
            build_windows()
        elif sys.argv[1] == "--macos":
            install_requirements()
            build_macos()
        elif sys.argv[1] == "--spec":
            install_requirements()
            build_with_spec()
        else:
            print("用法: python build.py [--windows|--macos|--spec]")
    else:
        # 默认使用spec文件打包
        install_requirements()
        build_with_spec()

if __name__ == "__main__":
    main()